package com.smallstep.hystrix.properties;

import com.smallstep.hystrix.enums.HystrixKeyEnum;
import org.springframework.core.env.Environment;

import java.io.InputStream;
import java.util.Properties;

/**
 * @author 李俊
 */
public class FFHystrixProperties {
    private static Environment environment;
    private static Properties properties;
    private static Properties defaultProperties = new Properties();

    private FFHystrixProperties() {
    }

    public static void setEnvironment(Environment env) {
        environment = env;
    }

    static {
        load();
    }

    public static void load() {
        try {
            properties = new Properties(defaultProperties);
            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("FFHystrix.properties");
            if (stream != null) {
                properties.load(stream);
                stream.close();
            }
        } catch (Exception var1) {

        }

    }

    public static Properties getProperties() {
        return properties;
    }

    public static Properties getDefaultProperties() {
        return defaultProperties;
    }

    public static String getProperty(String key) {
        if (environment.getProperty(key) != null) {
           return  environment.getProperty(key);
        }
        return properties.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if (environment.getProperty(key) != null) {
            return  environment.getProperty(key);
        }
        return properties.getProperty(key, defaultValue);
    }

    public static String getProperty(HystrixKeyEnum hystrixKeyEnum) {
        if (environment.getProperty(hystrixKeyEnum.getKey()) != null) {
            return  environment.getProperty(hystrixKeyEnum.getKey());
        }
        return properties.getProperty(hystrixKeyEnum.getKey(), hystrixKeyEnum.getDefaultValue());
    }

    public static void setProperty(String key, String value) {
        properties.setProperty(key, value);
    }

    public static int getPropertyForInteger(String key) {
        String value = getProperty(key);

        try {
            return Integer.parseInt(value);
        } catch (Exception var3) {
            throw new IllegalArgumentException("转换 \"" + value + "\" 为 int 过程发生错误，引发的 properties 属性为 " + key);
        }
    }

    public static int getPropertyForInteger(HystrixKeyEnum hystrixKeyEnum) {
        return getPropertyForInteger(hystrixKeyEnum.getKey(), hystrixKeyEnum.getDefaultValue());
    }

    public static int getPropertyForInteger(String key, String defaultValue) {
        String value = getProperty(key, defaultValue);

        try {
            return Integer.parseInt(value);
        } catch (Exception var4) {
            throw new IllegalArgumentException("转换 \"" + value + "\" 为 int 过程发生错误，引发的 properties 属性为 " + key);
        }
    }

    public static boolean getPropertyForBoolean(HystrixKeyEnum hystrixKeyEnum) {
        return getPropertyForBoolean(hystrixKeyEnum.getKey(), hystrixKeyEnum.getDefaultValue());
    }

    public static boolean getPropertyForBoolean(String key, String defaultValue) {
        return Boolean.parseBoolean(getProperty(key, defaultValue));
    }
}
